package com.walk.zworld.mvvm.vm

import androidx.lifecycle.LiveData
import com.base.lib.httpUtils.*
import com.walk.zworld.mvvm.repository.HomeRepository
import com.base.lib.base.event.SingleLiveEvent
import com.base.lib.base.mvvm.viewmodel.BaseRefreshViewModel

class HomeViewModel : BaseRefreshViewModel<Article>() {

    private val repository = HomeRepository()
    private val bannerDatas = SingleLiveEvent<List<Banner>>()
    private val searchData = SingleLiveEvent<ArticleResponseBody>()
    private val hotSearchData = SingleLiveEvent<MutableList<HotSearchBean>>()


    fun getBanner(): LiveData<List<Banner>> {
        launchUI {
            val result = repository.getBanner()
            bannerDatas.value = result.data
        }
        return bannerDatas
    }

    fun queryBySearchKey(page: Int, key: String): LiveData<ArticleResponseBody> {
        launchUI {
            val result = repository.queryBySearchKey(page, key)
            searchData.value = result.data
        }
        return searchData
    }

    fun getHotSearchData(): LiveData<MutableList<HotSearchBean>> {
        launchUI {
            val result = repository.getHotSearchData()
            hotSearchData.value = result.data
        }
        return hotSearchData
    }



    private val addCollect = SingleLiveEvent<Any>()
    private val addCollectOutside = SingleLiveEvent<Any>()
    private val cancelCollect = SingleLiveEvent<Any>()
    private val removeCollect = SingleLiveEvent<Any>()
    private var loginData = SingleLiveEvent<LoginData>()
    private var logoutData = SingleLiveEvent<Any>()
    private var registerData = SingleLiveEvent<LoginData>()
    private var data = SingleLiveEvent<TodoResponseBody>()
    /**
     * 如果需要对返回的数据进行操作后再返回给调用者
     * 可以使用Transformations.map(data) {}或 Transformations.switchMap(data){}对data进行处理
     * 详见 https://developer.android.google.cn/topic/libraries/architecture/livedata
     */
    fun getTodoList(page: Int, map: MutableMap<String, Any>): LiveData<TodoResponseBody> {
        launchUI {
            val res = repository.getTodoList(page, map)
            data.value = res.data
        }
        return data
    }

    fun addCollectArticle(id: Int): LiveData<Any> {
        launchUI {
            val res = repository.addCollectArticle(id)
            addCollect.value = res
        }
        return addCollect
    }

    fun addCollectOutsideArticle(
        title: String,
        author: String,
        link: String
    ): LiveData<Any> {
        launchUI {
            val res = repository.addCollectOutsideArticle(title, author, link)
            addCollectOutside.value = res
        }
        return addCollectOutside
    }

    fun cancelCollectArticle(id: Int): LiveData<Any> {
        launchUI {
            val res = repository.cancelCollectArticle(id)
            cancelCollect.value = res
        }
        return cancelCollect
    }

    fun removeCollectArticle(page: Int, originId: Int): LiveData<Any> {
        launchUI {
            val res = repository.removeCollectArticle(page, originId)
            removeCollect.value = res
        }
        return removeCollect
    }

    fun login(name: String, psw: String): LiveData<LoginData> {
        launchUI {
            val result = repository.login(name, psw)
            loginData.value = result.data
        }
        return loginData
    }

    fun logout(): LiveData<Any> {
        launchUI {
            val result = repository.logout()
            logoutData.value = result.data
        }
        return logoutData
    }

    fun register(name: String, psw: String, rePws: String): LiveData<LoginData> {
        launchUI {
            val result = repository.register(name, psw, rePws)
            registerData.value = result.data
        }
        return registerData
    }
















    override fun refreshData(params: Any?) {
        page = 0;
        var topArticlesList = mutableListOf<Article>()
        launchUI {
            topArticlesList = repository.getTopArticles().data
            topArticlesList.forEach {
                it.top = "1"
            }
        }
        launchUI {
            val elements = repository.getArticles(0).data.datas
            topArticlesList.addAll(elements)
            mData.value = topArticlesList
        }
        mData
    }

    override fun loadMore(params: Any?) {
        page += 1;
        launchUI {
            val result = repository.getArticles(page)
            mData.value = result.data.datas
        }
        mData
    }
}